package demo1;

public class Solution2 {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode a = headA ;
        ListNode b = headB ;

        int acount = size(headA) ;
        int bcount = size(headB) ;
        if(acount > bcount){
            while(acount - bcount > 0){
                a = a.next ;
                acount -- ;
            }
        }else{
            while(bcount - acount > 0){
                b = b.next ;
                bcount -- ;
            }
        }
        while(a != null && b != null){
            if(a == b){
                return a ;
            }
            a = a.next ;
            b = b.next ;
        }
        return null ;

    }
    public int size(ListNode head){
        ListNode ret = head ;
        int count = 0 ;
        while(ret != null){
            count ++ ;
            ret = ret.next ;
        }
        return count ;
    }

}